package com.guoqi.keepsign

import android.graphics.Typeface
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.view.WindowManager
import com.donkor.deepnight.utils.switchActivity
import com.guoqi.keepsign.mzt.MztTabUI
import kotlinx.android.synthetic.main.ui_splash.*
import java.lang.ref.WeakReference

/**
 * 启动页
 */
private val SWITCH_MAINACTIVITY = 1500//跳转到主界面
private val TAG: String? = "SplashActivity"

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.ui_splash)
        initView()
        MyHandler(this).sendEmptyMessageDelayed(0, SWITCH_MAINACTIVITY.toLong())

    }

    private class MyHandler(activity: SplashActivity) : Handler() {
        private val mActivity: WeakReference<SplashActivity> = WeakReference(activity)

        override fun handleMessage(msg: Message) {
            if (mActivity.get() == null) {
                return
            }
            val activity = mActivity.get()
            when (msg.what) {
                0 -> {
                    activity?.switchActivity<MztTabUI>()
                    activity?.finish()
                }
                else -> {
                }
            }
        }
    }

    private fun initView() {
        val font: Typeface = Typeface.createFromAsset(this.assets, "fonts/Lobster-1.4.otf")
        tv_splash_instruction.typeface = font
        tv_splash_instruction.text = "  Keep Sign\n打卡小草图"
    }

    override fun onDestroy() {
        MyHandler(this).removeCallbacksAndMessages(null)
        super.onDestroy()
    }
}